package com.zhangzhan.controller;

import com.alibaba.excel.EasyExcel;
import com.alibaba.fastjson.JSON;
import com.zhangzhan.domain.commcon.ResponseResult;
import com.zhangzhan.domain.dto.CategoryDto;
import com.zhangzhan.domain.dto.CategoryListDto;
import com.zhangzhan.domain.dto.SaveCategoryDto;
import com.zhangzhan.domain.entity.Category;
import com.zhangzhan.domain.enums.AppHttpCodeEnum;
import com.zhangzhan.domain.vo.CategoryVOs;
import com.zhangzhan.domain.vo.ExcelCategoryVo;
import com.zhangzhan.domain.vo.PageVO;
import com.zhangzhan.service.CategoryService;
import com.zhangzhan.utils.BeanCopyUtils;
import com.zhangzhan.utils.WebUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * @author 张一恒
 * @version 1.0
 * @description:
 * @date 2022/12/6 16:00
 */


@RestController
@RequestMapping("/content/category")
public class CategoryController {


    @Autowired
    private CategoryService categoryService;

    /**
     * @description 查询所有分类接口
     *
     * @return com.zhangzhan.domain.commcon.ResponseResult<com.zhangzhan.domain.vo.CategoryVOs>
     * @author 31115
     * @date 2022/12/6 16:17
     */
    @GetMapping("/listAllCategory")
    public ResponseResult<CategoryVOs> listAllCategory(){
        return categoryService.listAllCategory();
    }


    /**
     * @description 导出数据到excel
     * @param response
     * @return void
     * @author 31115
     * @date 2022/12/6 23:03
     */
    @GetMapping("/export")
    @PreAuthorize("@ps.hasPermissions('content:category:export')")
    public void export(HttpServletResponse response){
        try {
            //设置下载文件请求头
            WebUtils.setDownLoadHeader("分类文件.xlsx",response);
            //获取需要导出的数据
            List<Category> categoryList = categoryService.list();
            List<ExcelCategoryVo> excelCategoryVos = BeanCopyUtils.beanCopyUtil(categoryList, ExcelCategoryVo.class);
            //下载写到excel数据
            EasyExcel.write(response.getOutputStream(), ExcelCategoryVo.class)
                    .autoCloseStream(Boolean.FALSE) //关释放流
                    .sheet("分类数据导出")
                    .doWrite(excelCategoryVos); //导出文件实体类于封装实体类要类型相同
        } catch (Exception e) {
            //如果出现异常也要响应json
            ResponseResult result = ResponseResult.errorResult(AppHttpCodeEnum.SYSTEM_ERROR);
            WebUtils.renderString(response, JSON.toJSONString(result));
        }
    }

    /**
     * @description 分页查询
     * @param pageNum
     * @param pageSize
     * @param dto
     * @return com.zhangzhan.domain.commcon.ResponseResult<com.zhangzhan.domain.vo.PageVO>
     * @author 31115
     * @date 2022/12/8 16:30
     */
    @GetMapping("/list")
    public ResponseResult<PageVO> pageList(Integer pageNum, Integer pageSize, CategoryListDto dto){
        return categoryService.pageList(pageNum,pageSize,dto);
    }

    /**
     * @description 新增分类
     * @param dto
     * @return com.zhangzhan.domain.commcon.ResponseResult
     * @author 31115
     * @date 2022/12/8 16:40
     */
    @PostMapping
    public ResponseResult saveCategory(@RequestBody SaveCategoryDto dto){
        return categoryService.saveCategory(dto);
    }

    /**
     * @description 根据id查询分类
     * @param id 
     * @return com.zhangzhan.domain.commcon.ResponseResult<com.zhangzhan.domain.dto.CategoryDto>
     * @author 31115 
     * @date 2022/12/8 16:57
     */
    @GetMapping("{id}")
    public ResponseResult<CategoryDto> selectById(@PathVariable("id") Long id){
        return categoryService.selectById(id);
    }

    /**
     * @description 修改
     * @param dto
     * @return com.zhangzhan.domain.commcon.ResponseResult
     * @author 31115
     * @date 2022/12/8 16:58
     */
    @PutMapping
    public ResponseResult updateCategory(@RequestBody CategoryDto dto){
        return categoryService.updateCategory(dto);
    }

    /**
     * @description 删除
     * @param ids
     * @return com.zhangzhan.domain.commcon.ResponseResult
     * @author 31115
     * @date 2022/12/8 17:02
     */
    @DeleteMapping("{id}")
    public ResponseResult deleteCategory(@PathVariable("id") Long[] ids){
        return categoryService.deleteCategory(ids);
    }
}
